package org.jeecg.modules.yw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.yw.entity.Announcement;
import org.jeecg.modules.yw.entity.AnnouncementScope;
import org.jeecg.modules.yw.mapper.AnnouncementMapper;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.vo.AnnouncementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnnouncementService extends ServiceImpl<AnnouncementMapper, Announcement> {
    @Autowired
    private AnnouncementScopeService announcementScopeService;
    public void saveBatchAnnouncementScope(List<String> scope, String id) {
        for (String s : scope) {
            AnnouncementScope announcementScope = new AnnouncementScope();
            announcementScope.setAnnouncementId(id);
            announcementScope.setRoleId(Integer.parseInt(s));
            announcementScope.setId(CommonUtils.getID());
            announcementScope.setCreatetime(DateUtils.getCurrentDate());
            announcementScopeService.save(announcementScope);
        }
    }

    public void deleteAnnouncementScope(String id) {
        announcementScopeService.removeByAnnouncementId(id);
    }

    public IPage<AnnouncementVo> pageInfo(Page<Announcement> page) {
        QueryWrapper<Announcement> qw = new QueryWrapper<>();
        qw.orderByDesc("createtime");
        IPage<Announcement> result = this.page(page, qw);
        IPage<AnnouncementVo> resultVo = new Page<>();
        BeanUtils.copyProperties(result, resultVo);
        List<AnnouncementVo> records = result.getRecords().stream().map(announcement -> {
            AnnouncementVo announcementVo = new AnnouncementVo();
            BeanUtils.copyProperties(announcement, announcementVo);
            return announcementVo;
        }).collect(Collectors.toList());
        for (AnnouncementVo announcement : records) {
            QueryWrapper<AnnouncementScope> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("announcement_id", announcement.getId());
            List<String> scope = announcementScopeService.list(queryWrapper).stream().
                    map(AnnouncementScope::getRoleId).map(Object::toString).collect(Collectors.toList());
            announcement.setScope(scope);
        }
        resultVo.setRecords(records);
        return resultVo;
    }

    public IPage<Announcement> pageInfoByRoleId(int roleid, Page<Announcement> page, HttpServletRequest request) throws Exception {
        IPage<AnnouncementScope> resultVo = new Page<>(page.getCurrent(), page.getSize());
        QueryWrapper<AnnouncementScope> qw=new QueryWrapper<>();
        qw.eq("role_id",roleid);
        qw.orderByDesc("createtime");
        IPage<AnnouncementScope> result = announcementScopeService.page(resultVo, qw);
        List<String> records = new ArrayList<>(result.getRecords().size());
        for (int i = 0; i < result.getRecords().size(); i++) {
            records.add(result.getRecords().get(i).getAnnouncementId());
        }
        QueryWrapper<Announcement> qw1=new QueryWrapper<>();
        qw1.in("id",records);
        qw1.orderByDesc("createtime");
        IPage<Announcement> result1 = this.page(page, qw1);
        return result1;
    }

    public boolean removeB(String id) {
        announcementScopeService.removeByAnnouncementId(id);
        return this.removeById(id);
    }
}
